const { Lipei } = require("../models/lipeiMOdel");
//上传理赔信息
async function addLipei(req, res) {
    const { clientName, insuranceName, insurancePirce, imgname, payment, reason, salesId } = req.body;
    try {
        await Lipei.create({ clientName, insuranceName, insurancePirce, imgname, payment, reason, salesId });
        //移动图片
        // if (avatarImg) {
        //     moveFiles({
        //         formPath: './public/temp',
        //         toPath: './public/images',
        //         filename: avatarImg
        //     });
        //     removeFiles('./public/temp');
        // }
        res.send({
            message: "新增理赔成功",
            status: 1
        });
    } catch (error) {
        console.log('新增理赔失败', error);
        res.send({
            message: '新增理赔失败',
            status: 0,
        });
    }
};
//获取所有理赔信息
async function getAllLipei(req, res) {
    const { pageSize = 5, currentPage = 1 } = req.query;
    //总条数
    const total = await Sales.countDocuments();
    //总页数
    const totalPages = Math.ceil(total / pageSize);
    try {
        const data = await Lipei
            .find()
            .limit(pageSize - 0) //设置请求的条数
            .skip((currentPage - 1) * pageSize) //设置跳过的条数
            .populate('salesId')
        res.send({
            status: 1,
            message: '获取理赔成功',
            data: { total, totalPages, data, pageSize, currentPage }
        })
    } catch (error) {
        console.log('获取理赔失败',error);
        res.send({
            status:0,
            message:'获取理赔信息成功'
        })
    }
}
module.exports = { addLipei, getAllLipei};